// CurveViewer.cpp : implementation file
//
#include "stdafx.h"
#include "TinyCapture.h"
#include "CurveViewer.h"


// CurveViewer

IMPLEMENT_DYNAMIC(CurveViewer, CStatic)

CurveViewer::CurveViewer(int baseVal)
{
	isBufferInited  = false;
	mBaseValue = baseVal;
	mCurXPos = 0;
	mRedPen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
	mGreePen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
	mBluePen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
	mBufferImageDC.CreateCompatibleDC(NULL); 
	mStep = 2;
}

CurveViewer::~CurveViewer()
{
	mRedPen.DeleteObject();
	mGreePen.DeleteObject();
	mBluePen.DeleteObject();
	mBufferImage.DeleteObject();
	mBufferImageDC.DeleteDC();
}

BEGIN_MESSAGE_MAP(CurveViewer, CStatic)
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

void CurveViewer::OnPaint()
{
	CPaintDC dc(this); 
	dc.BitBlt(0, 0, mWndWidth, mWndHeight, &mBufferImageDC, 0, 0, SRCCOPY);
}

void CurveViewer::OnSize(UINT nType, int cx, int cy)
{
	CStatic::OnSize(nType, cx, cy);
}

void CurveViewer::setStep( int step /*= 1*/ )
{
	mStep = step;
}

void CurveViewer::drawColorLine( int nexY, CLRTYPE type )
{
	CDC* pDC = &mBufferImageDC;
	int currentYPos;
	switch (type)
	{
	case RED: 
		pDC->SelectObject(&mRedPen);
		currentYPos = mCurRedYPos;
		mCurRedYPos = nexY;
		break;
	case GREEN:
		pDC->SelectObject(&mGreePen);
		currentYPos = mCurGreenYPos;
		mCurGreenYPos = nexY;
		break;
	case BLUE:
		pDC->SelectObject(&mBluePen);
		currentYPos = mCurBlueYPos;
		mCurBlueYPos = nexY;
		break;
	default:
		return;
		break;
	}
	if (mCurXPos == 0)
	{
		restBkground(pDC);
		int nextXPos = mCurXPos + mStep;
		pDC->MoveTo(nextXPos, nexY);
		pDC->LineTo(nextXPos, nexY);
	}
	else if ((mCurXPos + mStep) >= mWndWidth)
	{
		resetVal();
	}
	else
	{
		int nextXpos = mCurXPos + mStep;
		pDC->MoveTo(mCurXPos, currentYPos);
		pDC->LineTo(nextXpos, nexY);
	}
}

int CurveViewer::calcualteDrawYPos( float dataVal )
{
	float per = dataVal/mBaseValue;
	int  midY = mWndHeight/2;
	int div = (int )(midY*per);
	return midY - div;
}

void CurveViewer::resetVal()
{
	int mid = mWndHeight/2;
	mCurXPos = 0;
}

void CurveViewer::restBkground(CDC* pDC)
{
	CRect rect;
	this->GetClientRect(&rect);
	pDC->FillSolidRect(&rect, RGB(255, 255 ,255));
}

void CurveViewer::setBaseValue( int baseVal )
{
	mBaseValue = baseVal;
}

BOOL CurveViewer::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
// 	return OnEraseBkgnd(pDC);
}

void CurveViewer::initBufferImage()
{
	CClientDC dc(this);
	mBufferImage.CreateCompatibleBitmap(&dc, mWndWidth, mWndHeight);
	mBufferImageDC.SelectObject(&mBufferImage);
	restBkground(&mBufferImageDC);
}

void CurveViewer::forwardXPos()
{
	mCurXPos += mStep;
}

void CurveViewer::drawCurve( float dataValue, int type )
{
	int nextYPos = calcualteDrawYPos(dataValue);
	switch (type)
	{
	case 0:
		drawColorLine(nextYPos, RED);
		break;
	case 1:
		drawColorLine(nextYPos, GREEN);
		break;
	case 2:
		drawColorLine(nextYPos, BLUE);
		break;
	default:
		break;
	}
}

void CurveViewer::initViewer()
{
	CRect rect;
	GetClientRect(&rect);
	mWndHeight = rect.Height();
	mWndWidth = rect.Width();
	mCurRedYPos = mWndHeight/2;
	mCurGreenYPos = mWndHeight/2;
	mCurBlueYPos = mWndHeight/2;
	if (!isBufferInited)
	{
		initBufferImage();
		isBufferInited = true;
	}
}
